/**
 * 一个卡片堆砌页面，如何应用建造者模式实现对每一个页面中结构卡片的创建
 * 
 * Q：对卡片堆砌的概念不是很清晰
 */


 /**
  * 礼包码，输入不同的码可以获得游戏币、娃娃、积分、复活币
  * 
  * 抽出一个礼包码的基类，属性有code
  * 细化游戏币、娃娃、积分、复活币的细节
  */
var arrToMap = function(arr, key) {
  var map = {};
  for (var i in arr) {
    map[key ? arr[i][key] : i] = arr[i];
  }
  return map;
};

var extenObject = function(extenObj) {
  for(var k in extenObj) {
    this[k] = extenObj[k];
  }
};

var GiftBag = function(code) {
    this.code = code || '';
};

var GameCoin = function(code) {
  var that = this;
  var codesArray = [
    {code: '1', count: 10},
    {code: '2', count: 6}
  ];//database
  (function(codesArray, code) {
    var result = arrToMap(codesArray, 'code')[code];
    extenObject.call(that, result);
  })(codesArray, code);
}
GameCoin.prototype.getCount = function() {
  return this.count;
}

var Doll = function(code) {
  var that = this;
  var codesArray = [
    {code: 1, name: '芭比', kind: ''}
  ];//database
  (function(codesArray, code) {
    var result = arrToMap(codesArray, 'code')[code];
    extenObject.call(that, result);
  })(codesArray, code);
}
Doll.prototype.getName = function() {
  return this.name;
}

var Point = function(code) {
  var that = this;
  var codesArray = [
    {code: '2', count: 6}
  ];//database
  (function(codesArray, code) {
    var result = arrToMap(codesArray, 'code')[code];
    extenObject.call(that, result);
  })(codesArray, code);
}
Point.prototype.getCount = function() {
  return this.count;
}

var ResurrectionCoin = function() {
  var that = this;
  var codesArray = [
    {code: '3', count: 6}
  ];//database
  (function(codesArray, code) {
    var result = arrToMap(codesArray, 'code')[code];
    extenObject.call(that, result);
  })(codesArray, code);
}
ResurrectionCoin.prototype.getCount = function() {
  return this.count;
}

var Code = function(code) {
  var _instance = new GiftBag(code);
  _instance.gameCoin = new GameCoin(code);
  _instance.doll = new Doll(code);
  _instance.point = new Point(code);
  _instance.resurrectionCoin = new ResurrectionCoin(code);
  return _instance;
}

var code = new Code(1);
console.log(111, code);
